package algorithm.collection;

import java.util.Iterator;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * @Author: Hevery
 * @Description: Map遍历的三种方式
 * @Date: Created on 2017/9/15
 * @Modified By:
 */
public class MapTest {



    public static void main(String[] args) {
        Map<Integer,String> map = new TreeMap<Integer, String>();
        map.put(1,"a");
        map.put(6,"s");
        map.put(3,"b");
        map.put(4,"e");
        map.put(5,"c");
        map.put(2,"v");


        /** 第一种方式：for-each遍历Entry */
        for(Map.Entry<Integer,String> entry : map.entrySet()){
            System.out.println("key="+entry.getKey()+"   value="+entry.getValue());
        }

        /** 第二种：for-each遍历keySet,values*/

        for(Integer key: map.keySet()){
            System.out.println("key = "+key);
        }

        for(String value : map.values()){
            System.out.println("value = "+value);

        }

        /** 第三种：f*/
        Iterator<Map.Entry<Integer,String>> entries = map.entrySet().iterator();
        while(entries.hasNext()){
            Map.Entry<Integer,String> entry = entries.next();
            System.out.println("key = "+entry.getKey()+" value = "+entry.getValue());
        }

    }
}
